//
//  SystemHelper.m
//  BSOrange
//
//  Created by kenshin on 14-9-1.
//  Copyright (c) 2014年 BazzarEntertainment. All rights reserved.
//

#import "SystemHelper.h"

//#import "BSWebViewController.h"

#import "SDWebImageManager.h"
#import "TKLoginController.h"
@interface SystemHelper ()

@end

@implementation SystemHelper


static SystemHelper *gSharedInstance = nil;

- (void)dealloc
{
    
}

+(SystemHelper *)sharedInstance
{
    static dispatch_once_t disonce;
    dispatch_once(&disonce, ^{
        gSharedInstance = [[SystemHelper alloc] init];
    });
    
    return gSharedInstance;
}


- (id)init
{
    if(self = [super init]) {
        
	}
	return self;
}

+ (void)OpenURL:(NSString*)url
{
//    BSWebViewController *webViewController = [[BSWebViewController alloc] initWithURL:[NSURL URLWithString:url]];
//
//    [[mAppDelegate tabController] presentViewController:[[UINavigationController alloc] initWithRootViewController:webViewController] animated:YES completion:nil];
}

#pragma mark - 设备信息,app版本等

+ (NSString *)appVersion
{
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
}

+ (NSString *)appBuildVersion
{
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"];
}

+ (NSString *)appName
{
    return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDisplayName"];
}

+ (NSString *)osVersion
{
    NSString* phoneVersion = [[UIDevice currentDevice] systemVersion];
    return phoneVersion;
}
#pragma mark - 工具等第三方配置


//
//+ (void)setupiRate
//{
//    /**
//     *  评分提醒
//     *  1. 启动次数:3+
//     *  2. 标识事件:暂不启用
//     *  3. 提醒周期:7天 （一周只会提醒一次）
//     */
//    if (BS_DEBUG) {//默认不设置会使用Info.plist读取到的
////        [iRate sharedInstance].applicationBundleID = @"com.charcoaldesign.rainbowblocks-free";
//    }
//    
//	[iRate sharedInstance].onlyPromptIfLatestVersion = NO;
//    
//    //set events count (default is 10)
//    //    [iRate sharedInstance].eventsUntilPrompt = 5;
//    [iRate sharedInstance].usesUntilPrompt = 3;
//    
//    //disable minimum day limit and reminder periods
//    [iRate sharedInstance].daysUntilPrompt = 0;
//    [iRate sharedInstance].remindPeriod = 7;
//    
//    //禁用 永不提示功能
//    [iRate sharedInstance].cancelButtonLabel = @"";
//    
//    [iRate sharedInstance].verboseLogging = BS_DEBUG;
//}

/* 版本更新检测
+ (void)setupVersionUpdateCheck
{
//    [MobClick checkUpdate];
//
    [[Harpy sharedInstance] setAppID:[NSString stringWithFormat:@"%d",APPID]];
    [[Harpy sharedInstance] setAppName:APPNAME];
    [[Harpy sharedInstance] setAlertType:HarpyAlertTypeOption];
    [[Harpy sharedInstance] setDelegate:[SystemHelper sharedInstance]];
    
}

+ (void)fireVersionCheck
{
    [[Harpy sharedInstance] checkVersionDaily];
}
*/

+ (void)presentLoginViewController {
    TKLoginController *vc = [[TKLoginController alloc] init];
    KSNavigationController *nav = [[KSNavigationController alloc] initWithRootViewController:vc];
    
    nav.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:[BLColorConfig blackColor], NSFontAttributeName:[UIFont systemFontOfSize:18]};
//    nav.navigationBar.tintColor = [BLColorConfig mainColor]
    nav.navigationItem.leftBarButtonItem.tintColor = [BLColorConfig blackColor];
    [nav.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
    nav.navigationBar.shadowImage = [UIImage new];
//    nav.navigationBar.translucent = YES;
    
    [[KSNavi defaultNavi] presentViewController:nav animated:YES completion:^{
        
    }];
}

+ (NSString *)buildFileNameByCurrentTimeAndUUID {
    return [[NSString stringWithFormat:@"%@_%@", [NSDate jc_currentDateWithFormat:@"yyyy-MM-dd-hh:mm:ss"], [[[UIDevice currentDevice] identifierForVendor] UUIDString]] jc_md5];
}


+ (BOOL)checkAndShowLoginViewControllerIfNotLogin {
    BOOL isLogin = [TKAccountManager isLogin];
    if (!isLogin) {
        [self presentLoginViewController];
    }
    
    return isLogin;
}


#pragma mark - Other

/**
 *  不同版本需要做的一些兼容处理
 */
+ (void)handelAppVersionDiff
{
    //TODO... 版本判断，是否第一次，需要做一些数据迁移等可能的兼容
    
//    //用户信息不在缓存在TMCache里，迁移至NSKeyedArchiver直接保存在Document里
//    if ([[TMCache sharedCache] objectForKey:@"BS_MODEL_USER"]) {
//        [NSKeyedArchiver archiveRootObject:[[TMCache sharedCache] objectForKey:@"BS_MODEL_USER"] toFile:UserModelCache];
//        [[TMCache sharedCache] removeObjectForKey:@"BS_MODEL_USER"];
//    }
    
}

+ (void)preloadImage:(NSString *)urlString
{
    [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:urlString]
                                                    options:SDWebImageLowPriority
                                                   progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                                                       
                                                   } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                                                       
                                                   }];
}

+ (void)preloadImages:(NSArray *)url
{
    [url enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
        NSString *urlString = obj;
        
        [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:urlString]
                                                   options:SDWebImageLowPriority
                                                  progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                                                      
                                                  } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
                                                      
                                                  }];
    }];
}

@end
